package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Sdr
 * @since 2021-02-06
 */
@Api(value = "讲师Controller", tags = "讲师管理接口")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin //解决跨域问题
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    //1. 查询讲师表所有数据 rest风格
    @ApiOperation("查询所有讲师")
    @GetMapping("findAll")
    public R findAllTeacher() {

        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }

    //2. 逻辑删除讲师方法
    @ApiOperation("逻辑删除一个讲师")
    @DeleteMapping("{id}")//通过路径传递
    public R removeTeacher(@PathVariable String id) {

        boolean b = teacherService.removeById(id);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }

    }


    /**
     * 分页查询方法
     *
     * @return
     */
    @ApiOperation("查询讲师并分页")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current, @PathVariable long limit) {


        //分页对象
        Page<EduTeacher> eduTeacherPage = new Page<>(current, limit);
        //分页方法
        teacherService.page(eduTeacherPage, null);

        //总记录数
        long total = eduTeacherPage.getTotal();

        //老师的数据list
        List<EduTeacher> records = eduTeacherPage.getRecords();


        return R.ok().data("total", total).data("rows", records);
    }


    @ApiOperation("查询讲师并分页附带模糊条件")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) TeacherQuery teacherQuery) {

        //分页对象
        Page<EduTeacher> eduTeacherPage = new Page<>(current, limit);
        //条件构造
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        /*
         * 判断是否为空，不为空则拼接条件
         * */
        if (!StringUtils.isEmpty(name)) {

            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {

            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            //大于等于
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            //小于等于
            queryWrapper.le("gmt_modified", end);
        }

        //添加排序
        queryWrapper.orderByDesc("gmt_create");

        //调用查询方法
        teacherService.page(eduTeacherPage, queryWrapper);

        //总记录数
        long total = eduTeacherPage.getTotal();

        //老师的数据list
        List<EduTeacher> records = eduTeacherPage.getRecords();


        return R.ok().data("total", total).data("rows", records);
    }

    //添加讲师接口
    @PostMapping("addTeacher")
    @ApiOperation("添加讲师接口")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);

        if (save) {
            return R.ok();
        } else {
            return R.error();
        }

    }

    //根据id查询讲师
    @GetMapping("getTeacher/{id}")
    @ApiOperation("根据id查询讲师")
    public R getTeacher(@PathVariable String id) {
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }


    //修改讲师
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {

        boolean b = teacherService.updateById(eduTeacher);

        if (b) {
            return R.ok();
        } else {
            return R.error();
        }

    }

}

